package com.yonyou.gmmc.service.maintain.controller;

import org.apache.log4j.Logger;
import org.marketing.common.dto.MQReceiveResult;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.enums.EventConstant.EventBizStep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.service.maintain.constant.MaintainConstant;
import com.yonyou.gmmc.service.maintain.service.MiantainMqService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = MaintainConstant.BASE_PATH + "/" + MaintainConstant.VERSION)
@Api("履历信息")
public class MaintainMqController {
	private Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private MiantainMqService miantainMqService;

	/**
	 * 维修预约信息发送
	 * 
	 * @param event
	 * @return
	 */
	@RequestMapping(value = "/IC001", method = RequestMethod.POST)
	@ApiOperation(value = "接收MQ传来的关键事件", notes = "维修预约信息发送")
	public MQReceiveResult sendApplyInfo(@RequestBody BizEvent event) {
		logger.info("---维修预约信息发送业务开始----");
		MQReceiveResult result = new MQReceiveResult();
		try {
			result = miantainMqService.sendApplyInfo(event);
		} catch (Exception e) {
			logger.error("维修预约信息发送;error:" + e.getMessage());
			result.setResult("500");
			result.setMessage(e.getMessage());
		}
		return result;
	}

	@RequestMapping(value = "/event", method = RequestMethod.POST)
	@ApiOperation(value = "接收MQ传来的关键事件", notes = "维修预约信息发送")
	public MQReceiveResult receiveEvent(@RequestBody BizEvent event) {
		logger.info("---维修预约信息发送业务开始----");
		MQReceiveResult result = new MQReceiveResult();
		if (event.getEventBizStep().equals(EventBizStep.REGISTER_RECORD_SYNC)) {
			// 履历同步
			return sendMaintainInfo(event);
		} /*
			 * else if (event.getEventBizStep().equals(EventBizStep.
			 * REPAIR_APPOINTMENT_SEND)) { // 预约发送 return sendApplyInfo(event);
			 * } else if
			 * (event.getEventBizStep().equals(EventBizStep.RECOMMAND_INFO_SEND)
			 * ) { // 潜客信息发送 return sendUserInfo(event); } else if
			 * (event.getEventBizStep().equals(EventBizStep.
			 * RECOMMAND_CARORDER_INFO_SEND)) { // 购车信息发送 return
			 * sendCarInfo(event); }
			 */
		result.setResult("200");
		result.setMessage("--receiveEvent--null--");
		return result;
	}

	/**
	 * 从第三方获取维修履历信息
	 * 
	 * @param event
	 * @return
	 */
	@RequestMapping(value = "/IC004", method = RequestMethod.POST)
	@ApiOperation(value = "接收MQ传来的关键事件", notes = "接收事件进行维修履历同步")
	public MQReceiveResult sendMaintainInfo(@RequestBody BizEvent event) {
		logger.info("---维修履历同步业务开始----");
		MQReceiveResult result = new MQReceiveResult();
		try {
			result = miantainMqService.addMaintainRecord(event);
		} catch (Exception e) {
			logger.error("维修履历同步异常;error:" + e.getMessage());
			result.setResult("500");
			result.setMessage(e.getMessage());
		}
		return result;
	}

	/**
	 * 潜客留资信息发送
	 * 
	 * @param event
	 * @return
	 */
	@RequestMapping(value = "/IC005", method = RequestMethod.POST)
	@ApiOperation(value = "接收MQ传来的关键事件", notes = "潜客留资信息发送")
	public MQReceiveResult sendUserInfo(@RequestBody BizEvent event) {
		logger.info("---潜客留资信息发送业务开始----");
		MQReceiveResult result = new MQReceiveResult();
		try {
			result = miantainMqService.sendUserInfo(event);
		} catch (Exception e) {
			logger.error("潜客留资信息发送;error:" + e.getMessage());
			result.setResult("500");
			result.setMessage(e.getMessage());
		}
		return result;
	}

	/**
	 * 推荐购车信息发送
	 * 
	 * @param event
	 * @return
	 */
	@RequestMapping(value = "/IC006", method = RequestMethod.POST)
	@ApiOperation(value = "接收MQ传来的关键事件", notes = "推荐购车信息发送")
	public MQReceiveResult sendCarInfo(@RequestBody BizEvent event) {
		logger.info("---推荐购车信息发送业务开始----");
		MQReceiveResult result = new MQReceiveResult();
		try {
			result = miantainMqService.sendCarInfo(event);
		} catch (Exception e) {
			logger.error("推荐购车信息发送;error:" + e.getMessage());
			result.setResult("500");
			result.setMessage(e.getMessage());
		}
		return result;
	}

}
